package com.cc.dao.impl;

import com.cc.dao.AttendanceDao;
import com.cc.dao.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author CC
 * @version 1.0
 * @description: TODO
 * @date 2025/3/31 9:43
 */
public class AttendanceDaoImpl extends BaseDao implements AttendanceDao {
    @Override
    public boolean isClockIn(String employeeId) {
        String sql = "select count(1) count from attendance_records where employee_id ="+employeeId +
                " and date = DATE_FORMAT(CURDATE(),'%Y-%m-%d')";
        ResultSet resultSet = executeQuery(sql);
        if (resultSet != null){
            try {
                while (resultSet.next()){
                   return resultSet.getInt("count")==1;
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                closedAll(resultSet);
            }
        }

        return false;
    }

    @Override
    public void clockIn(String employeeId) {
        StringBuffer sb = new StringBuffer();
        sb.append(" INSERT INTO attendance_records (employee_id, check_in_time, `status`, date)");
        sb.append("("+employeeId);
        sb.append(" ,TIME(NOW()),");
        sb.append(" CASE ");
        sb.append(" WHEN TIME(NOW()) > '09:00:00' THEN '迟到'");
        sb.append(" ELSE '正常'");
        sb.append(" END, ");
        sb.append(" DATE_FORMAT(CURDATE(),'%Y-%m-%d')  ");
        executeUpdate(sb.toString());
    }

    @Override
    public void updateClockIn(String employeeId) {
        StringBuffer sb = new StringBuffer();
        sb.append(" UPDATE attendance_records SET check_out_time = TIME(NOW())");
        sb.append(" WHERE employee_id = "+employeeId);
        sb.append(" AND date = DATE_FORMAT(CURDATE(),'%Y-%m-%d')");
        executeUpdate(sb.toString());
    }
}
